package com.krain.ddbb.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.webkit.SslErrorHandler;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.krain.ddbb.R;
import com.krain.ddbb.base.BaseActivity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;


/**
 * Class description
 * 公用浏览器页面
 * pms地址：http://pms.ipo.com/pages/viewpage.action?pageId=12167257
 *
 * @author yamin
 * @date 2015/8/19 下午 05:32
 */
@EActivity(R.layout.activity_browser)
public class BrowserActivity extends BaseActivity {

    public static final int GET = 0; //get请求type
    public static final int POST = 1;//post请求type

    private final static int REQUSTCODE_LOGIN = 3;//登录请求码

    private static final int FILECHOOSER_RESULTCODE = 1;
    private static final int CAMERAL_REQUEST_CODE = 2;
    private static final String TEMP_AVATAR_NAME = "temp.jpg";
    public String filePath = null;

    private ValueCallback mUploadMessage;


    @ViewById(R.id.pub_browser_webview)
    WebView mWebview;


    @Extra("title")
    String mTitle;
    @Extra("url")
    String mUrl;
    @Extra("type")
    int type = GET;
    @Extra("params")
    String params;
    @Extra("canGoBack")
    boolean mCanGoBack = true;
    @Extra("setReceivedTitle")
    boolean setReceivedTitle = true;
    @Extra("urlTittle")
    String urlTittle;


    @Extra("showMsg")
    boolean needShowMsg;

    @Override
    protected String getToolbarTitle() {
        return "叮叮梆梆";
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebview.canGoBack()) {
            //  finishFirstUrl(setReceivedTitle);
            mWebview.goBack();
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }


    public static void jumpToThisActivity(BaseActivity activity, String url, String params, int type, boolean mCanGoBack) {
        jumpToThisActivity(activity, url, params, type, mCanGoBack, true, null);
    }

    /**
     * @param activity
     * @param url
     * @param params
     * @param type
     * @param mCanGoBack
     * @param setReceivedTitle 是否允许app自动抓取html Dom节点的<head>元素中<title>作为app当前页面的title
     * @param urlTittle        固定显示的tittle
     */
    public static void jumpToThisActivity(BaseActivity activity, String url, String params, int type, boolean mCanGoBack, boolean setReceivedTitle, String urlTittle) {
        Intent intent = new Intent(activity, BrowserActivity_.class);
        intent.putExtra("title", "加载中");
        intent.putExtra("url", url);
        intent.putExtra("params", params);
        intent.putExtra("type", type);
        intent.putExtra("canGoBack", mCanGoBack);
        intent.putExtra("setReceivedTitle", setReceivedTitle);
        intent.putExtra("urlTittle", urlTittle);

        activity.startActivity(intent);
    }

    public static void jumpToThisActivity(BaseActivity activity, String url) {
        jumpToThisActivity(activity, url, null, GET, false);
    }

    @AfterViews
    void afterView() {
        initWebSetting();
        loadUrl();
    }


    void loadUrl() {
        if (TextUtils.isEmpty(mUrl)) {
            baseUtil.showSnackBar(this, "url为空");
            return;
        }
        if (type == POST) {
            mWebview.postUrl(mUrl, params.getBytes());
        } else {
            mWebview.loadUrl(mUrl);
        }
    }

    void initWebSetting() {
        mWebview.setWebChromeClient(webChromeClient);
        mWebview.setWebViewClient(webViewClient);

    }

    WebViewClient webViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }


        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            finish();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (setReceivedTitle) {
                setTitle(view.getTitle());
            }
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }
    };

    WebChromeClient webChromeClient = new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {

            super.onProgressChanged(view, newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            if (setReceivedTitle) {
                setTitle(title);
            }
        }


        public void loadUrl(String url) {
            mWebview.loadUrl(url);
        }


    };
}

